﻿using System;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The parent abstract class for the tools for geosketch
    /// </summary>
    public abstract class BaseTool
    {
        #region Fields
        /// <summary>
        /// The viewport on which the tool will work
        /// </summary>
        public BaseViewport Viewport;

        /// <summary>
        /// The name of the tool
        /// </summary>
        public string Name;

        /// <summary>
        /// The status strip drop down button associated to the the tool
        /// The drop down contains a textbox used to send text command to the tool
        /// </summary>
        public ToolStripDropDownItem StatusStripDropDownButton;

        /// <summary>
        /// The label on which the tool can display a message to the user
        /// </summary>
        public ToolStripLabel StatusStripLabelMessages;

        /// <summary>
        /// The toolbar button affected to the tool
        /// </summary>
        public BaseToolbarButton ToolbarButton;
        #endregion

        #region Constructor
        /// <summary>
        /// The base class for all tools, is not directly instantiated
        /// </summary>
        /// <param name="Viewport">The viewport the tool with act on</param>
        /// <param name="Name">The name of the tool</param>
        public BaseTool(BaseViewport Viewport, string Name)
        {
            this.Viewport = Viewport;
            this.Name = Name;
        }
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public virtual void MouseDown(MouseEventArgs MouseArgs)
        {
        }

        /// <summary>
        /// The method called when the mouse button is up
        /// </summary>
        public virtual void MouseUp(MouseEventArgs MouseArgs)
        {
        }

        /// <summary>
        /// The method called when the mouse is moved
        /// </summary>
        public virtual void MouseMove(MouseEventArgs MouseArgs)
        {
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public virtual void TextEntry(string Text)
        {
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public virtual void ExternalPointSent(Point2D64D Point)
        {
        }
        #endregion

        #region Methods
        /// <summary>
        /// Refreshes the viewport
        /// </summary>
        public void RefreshViewport()
        {
            Viewport.DisplayControl.Refresh();
        }

        /// <summary>
        /// Draw the graphics requested by the object
        /// </summary>
        public virtual void Draw()
        {
        }

        /// <summary>
        /// Gets a point from a coordinates string (X.x;Y.y)
        /// </summary>
        protected Point2D64D GetCoordinatesFromString(string Text)
        {
            Point2D64D point = new Point2D64D();

            try
            {
                string[] TextSplit = Text.Split(Vars.Setup.GeoSketch.CoordinatesSeparator);
                point.X = Convert.ToDouble(TextSplit[0].Replace(',', '.'), Vars.Setup.GeoSketch.NumberFormat);
                point.Y = Convert.ToDouble(TextSplit[1].Replace(',', '.'), Vars.Setup.GeoSketch.NumberFormat);

                return point;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// The tool can call this method to show the textbox
        /// </summary>
        public void ShowTextInput()
        {
            if (StatusStripDropDownButton != null)
            {
                StatusStripDropDownButton.ShowDropDown();
                ((ToolStripTextBox)StatusStripDropDownButton.DropDownItems[0]).Focus();
            }
        }

        /// <summary>
        /// The tool can call this method to display a message to the user
        /// </summary>
        public void SendMessage(string Message)
        {
            if (StatusStripLabelMessages != null)
            {
                StatusStripLabelMessages.Text = Message;
            }
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public virtual void SendFirstMessage()
        {
            SendMessage("");
        }

        /// <summary>
        /// This method is called when deselcting the tool, or swithcing to another one
        /// </summary>
        public virtual void Release()
        {
        }
        #endregion
    }
}
